package com.gitee.qdbp.general.common.biz.files.service.wantu;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.alibaba.media.MediaConfiguration;
import com.alibaba.media.manage.ManageClient;
import com.alibaba.media.upload.UploadClient;
import com.alibaba.media.upload.UploadPolicy;
import com.alibaba.media.upload.UploadTokenClient;
import com.alibaba.media.upload.impl.DefaultUploadClient;
import com.alibaba.media.upload.impl.DefaultUploadTokenClient;

public class SimpleClientFactory implements MediaClientFactory {

    private MediaConfiguration configuration;

    private UploadTokenClient tokenClient;
    private UploadClient uploadClient;
    private ManageClient manageClient;
    private UploadPolicy uploadPolicy;

    private static Map<String, DefaultUploadClient> clientMap = new ConcurrentHashMap<>();

    public SimpleClientFactory(MediaConfiguration configuration) {
        this.configuration = configuration;

        tokenClient = new DefaultUploadTokenClient(configuration);
        uploadClient = clientMap.get(configuration.getUploadEndpoint());
        if (uploadClient == null) {
            synchronized (this) {
                uploadClient = new DefaultUploadClient(configuration.getUploadEndpoint());
            }
        }
        manageClient = new SimpleManageClient(configuration);

        uploadPolicy = new UploadPolicy();
        uploadPolicy.setNamespace(configuration.getNamespace());
        uploadPolicy.setExpiration(-1);
    }

    /** {@inheritDoc} **/
    @Override
    public MediaConfiguration getConfiguration() {
        return configuration;
    }

    /** {@inheritDoc} **/
    @Override
    public UploadTokenClient getTokenClient() {
        return tokenClient;
    }

    /** {@inheritDoc} **/
    @Override
    public UploadClient getUploadClient() {
        return uploadClient;
    }

    /** {@inheritDoc} **/
    @Override
    public ManageClient getManageClient() {
        return manageClient;
    }

    /** {@inheritDoc} **/
    @Override
    public UploadPolicy getUploadPolicy() {
        return uploadPolicy;
    }

}
